/**
 * Created by lixiaodi on 2019/03/04.
 */
var states = [{code: 1, text: '开启' }, {code: 2, text: '关闭'}];

mini.parse();
var form = new mini.Form("form1");
var stateInput = mini.getByName('state');
//保存
function SaveData() {
	
    form.validate();
    if (form.isValid() == false) return;
    var ids = form.getData();
    console.log(ids);
    var button = {
    	buttonId : ids.buttonId,
    	buttonText : ids.buttonText,
    	state : ids.state,
    	showIndex: ids.showIndex,
    	buttonCode : ids.buttonCode,
        entityName:"Buttons"
        //createDate : ids.createDate
    };
//    findByButtonText();
//    findByButtonCode();
    console.log(button);
    button = JSON.stringify(button);
    $.ajax({
        url: ajaxUrl + "/buttons/saveButtons.do",
        type: 'POST',
        data: button,
        contentType : "application/json",
        success: function (data) {
            CloseWindow("save");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            CloseWindow();
        }
    });
}
//编辑/新增
function SetData(data) {
	//编辑
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        var buttonId = data.id;
        $.ajax({
            url: ajaxUrl + "/buttons/findById.do?id=" + buttonId,
            type: 'POST',
            success: function (data) {
                var o = mini.decode(data);
                //日期格式转化
                if(o.data.createDate){
                	 o.data.createDate = formatDate(o.data.createDate);
                }else{
                	o.data.createDate = '';
                }
                //表单赋值
                form.setData(o.data);
                form.setChanged(false);
            },
            error:function(data){
                mini.alert('失败');
            }
        });
    //新增
    }else if(data.action == "new"){
        console.log('new');
        //新增时状态默认开启
        stateInput.setValue(states[0].code);
        stateInput.setText(states[0].text);
    }
}

function GetData() {
    var o = form.getData();
    return o;
}
//关闭子页面
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//校验按钮名称是否重复
function findByButtonText(){
	
	var ids = form.getData();
	
	var buttonText = ids.buttonText;
	
	$.ajax({
		url 		: ajaxUrl + '/buttons/findByButtonsName.do',
		type		: 'POST',
		data    	: {
			buttonText : buttonText
		},
		success		: function(data){
			if(data.state == 'error'){
				mini.alert(data.message);
				mini.get('buttonText').setValue('');
			}
		},
		error       : function(message){
		}
	})
}
//校验code是否重复
function findByButtonCode(){
	
	var ids = form.getData();
	var buttonCode = ids.buttonCode;
	$.ajax({
		url         : ajaxUrl + '/buttons/findByButtonCode.do',
		type        : 'POST',
		data		: {
			buttonCode : buttonCode
		},
		success		: function(data) {
			if(data.state == 'error'){
				mini.alert(data.message);
				mini.get('buttonCode').setValue(''); 

			}
		},
		error       : function(message){
		}
		
	})
	
}
//校验模块必输
function onNotSpaceValidation(e) {
    if (e.isValid) {
        if (!e.value) {
            e.errorText = "不能为空";
            e.isValid = false;
        }
    }
}


